import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex);
const store = new Vuex.Store({
    state: {
        count: 0,
        list: [],
         // 购物列表数据
        shopAllList:[],
        // 购物车数据
        ShopCarList:[]
    },
    mutations: {
        addCount(state) {
            state.count++
        },
        //数组传值
        setList(state, data) {
            console.log('data', data)
            state.list.push(data)
        },
         // 增加购物列表数据
        addShopList2(state,shopData){
         state.shopAllList.push(shopData)
        },
         // 增加购物车列表数据
        addShopCarList(state,shopData){
           state.ShopCarList.push(shopData)
         },
         addCount (state, shopId) {
            // 通过传进来的参数找到该商品的下标
            let index = state.ShopCarList.findIndex(item => {
              return item.id === shopId
            })
      
            // 通过下标拿到商品的数量，并且自增
            state.ShopCarList[index].count++
          },
          deleteCount (state, shopId) {
            let index = state.ShopCarList.findIndex(item => {
              return item.id === shopId
            })
      
            if (state.ShopCarList[index].count === 1) {
              return
            }
      
            state.ShopCarList[index].count--
          },


          deleCount(state,data){
          state.ShopCarList.pop(data)
          }
        }
      })
export default store

